#include "../include/GLUI.h"




void initialize_live_variables(void);
void glui_cb(int control);
void beginFullScreen();
void endFullScreen();



// pointers for all of the glui controls
GLUI *glui;
GLUI_Rollout		*scene_rollout;
GLUI_Rollout		*projection_rollout;
GLUI_Rollout		*wii_rollout;
GLUI_Rollout		*camera_rollout;

GLUI_Panel		*object_panel;
GLUI_Panel		*transform_panel;
GLUI_Panel		*camera_panel;
GLUI_Panel		*wiimote1_panel;
GLUI_Panel		*wiimote1_extra_panel;
GLUI_Panel		*wiimote2_panel;
GLUI_Panel		*wiimote2_extra_panel;
GLUI_Panel		*wii_tracker_panel;

GLUI_RadioGroup		*object_type_radio;
GLUI_RadioGroup		*projection_radio;
GLUI_RadioGroup		*pick_radio;

 GLUI_Spinner		*eye_x_spin;
 GLUI_Spinner		*eye_y_spin;
 GLUI_Spinner		*eye_z_spin;
 GLUI_Spinner		*lookat_x_spin;
 GLUI_Spinner		*lookat_y_spin;
 GLUI_Spinner		*lookat_z_spin;
 GLUI_Spinner		*dist1_spin;
 GLUI_Spinner		*dist2_spin;
 GLUI_Spinner		*dist3_spin;

 GLUI_Spinner		*nearplane_spin;
 GLUI_Spinner		*focal_length_spin;
 GLUI_Spinner		*farplane_spin;
 GLUI_Spinner		*light_intensity_spin;
 GLUI_Spinner		*IOD_spin;
 GLUI_Spinner		*FOVY_spin;

 GLUI_Translation	*nearplane_trans;
 GLUI_Translation	*focal_length_trans;
 GLUI_Translation	*farplane_trans;
 GLUI_Translation	*light_intensity_trans;
 GLUI_Translation	*IOD_trans;
 GLUI_Translation	*FOVY_trans;

GLUI_Spinner		*LED1_x_spin;
GLUI_Spinner		*LED1_y_spin;
GLUI_Spinner		*LED2_x_spin;
GLUI_Spinner		*LED2_y_spin;
GLUI_Spinner		*LED3_x_spin;
GLUI_Spinner		*LED3_y_spin;
GLUI_Spinner		*Pitch;
GLUI_Spinner		*Roll;
GLUI_Spinner		*Eye_base_x;
GLUI_Spinner		*Eye_base_y;
GLUI_Spinner		*Eye_base_z;
GLUI_Spinner		*Lookat_base_x;
GLUI_Spinner		*Lookat_base_y;
GLUI_Spinner		*Lookat_base_z;

GLUI_Checkbox *draw_floor;
GLUI_Checkbox *draw_object;
GLUI_Checkbox *stereo_view;
GLUI_Checkbox *full_screen;
GLUI_Checkbox *use_depth_buffer;
GLUI_Checkbox *wiimote1_connected;
GLUI_Checkbox *wiimote2_connected;
GLUI_Checkbox *LED1_1;
GLUI_Checkbox *LED1_2;
GLUI_Checkbox *LED1_3;
GLUI_Checkbox *LED1_4;
GLUI_Checkbox *LED2_1;
GLUI_Checkbox *LED2_2;
GLUI_Checkbox *LED2_3;
GLUI_Checkbox *LED2_4;

int live_object_type;	
int live_projection_type;
int live_pick_type;
float live_object_rotation[16];
float live_nearplane_trans;
float live_focal_length;
float live_farplane_trans;
float live_IOD_trans;
float live_FOVY_trans;
float live_light_intensity_trans;
float live_dist[3];

int live_draw_floor;
int live_draw_object;
int live_stereo_view;
int live_prevent_floor_culling;
int live_use_depth_buffer;

void GLUIInit(void)
{
	GLUI_Master.set_glutReshapeFunc(myGlutReshape);
	GLUI_Master.set_glutIdleFunc(myGlutIdle);
	GLUI_Master.set_glutKeyboardFunc(myGlutKeyboard);
	//GLUI_Master.set_glutDisplayFunc(myGlutDisplay);
	//GLUI_Master.set_glutMouseFunc(myGlutMouse);
	//glutMotionFunc(myGlutMotion);


	SetupSubwindow();

	GLUI_Master.get_viewport_area(&subwindow_x, &subwindow_y, &subwindow_width, &subwindow_height);


	// initialize live variables
	initialize_live_variables();


}

// some controls generate a callback when they are changed
void glui_cb(int control)
{

	switch(control)
	{
	case CB_DEPTH_BUFFER:
		if (live_use_depth_buffer)
			glEnable(GL_DEPTH_TEST);
		else
			glDisable(GL_DEPTH_TEST);
		break;
	case CB_ACTION_BUTTON:
		break;
	case CB_RESET:
		initialize_live_variables();
		break;
	case CB_FULL_SCREEN:
		full_screen->set_int_val(!full_screen->get_int_val());
		ToggleFullScreen();
		break;
	}

	glutPostRedisplay();
}



void ToggleFullScreen()
{
	if(!full_screen->get_int_val()) {
		beginFullScreen();
		full_screen->set_int_val(1);
	}
	else {
		endFullScreen();
		full_screen->set_int_val(0);
	}
}

void beginFullScreen()
{
	glui->hide();
	old_window_width = window_width;
	old_window_height = window_height;
	//glutGameModeString("1024x768");
	//glutEnterGameMode();
	glutPositionWindow(0, 0);
	glutReshapeWindow(1024 * 2, 768);
}

void endFullScreen()
{
	glui->show();
	//glutLeaveGameMode();
	glutReshapeWindow(old_window_width, old_window_height);
	glutPositionWindow(100, 100);
}



void initialize_live_variables(void)
{
	live_object_type = 0;
	live_light_intensity_trans = 100.0;
	live_draw_floor = 1;
	live_draw_object = 1;
	live_stereo_view = 0;
	live_projection_type = 1;

	live_nearplane_trans = 1.0;
	live_focal_length = 40.0;
	live_farplane_trans = 100.0;
	live_IOD_trans = 0.5;
	live_FOVY_trans = 90.0;

	for(int i = 0; i < 15; i++)
		live_object_rotation[i] = 0;
	live_object_rotation[0] = 1;
	live_object_rotation[5] = 1;
	live_object_rotation[10] = 1;
	live_object_rotation[15] = 1;

}

void SetupSubwindow()
{
	//
	// create the interface subwindow and add widgets
	//
	glui = GLUI_Master.create_glui_subwindow(main_window, GLUI_SUBWINDOW_LEFT);

	// quit button
	glui->add_button("Quit", 0, (GLUI_Update_CB)exit);

	// empty space
	glui->add_statictext("");

	// the scene rollout - holds all scene control widgets
	scene_rollout = glui->add_rollout("Scene");
	object_panel = glui->add_panel_to_panel(scene_rollout, "", GLUI_PANEL_NONE);
	// the object type radio buttons
	object_type_radio = glui->add_radiogroup_to_panel(object_panel, &live_object_type);
	glui->add_radiobutton_to_group(object_type_radio, "Teapot");
	glui->add_radiobutton_to_group(object_type_radio, "Cone");
	glui->add_radiobutton_to_group(object_type_radio, "Dodecahedron");
	glui->add_radiobutton_to_group(object_type_radio, "Icosahedron");
	glui->add_radiobutton_to_group(object_type_radio, "Sphere");
	glui->add_radiobutton_to_group(object_type_radio, "Torus");
	// the camera information spinners
	glui->add_column_to_panel(object_panel, false);
	nearplane_spin		= glui->add_spinner_to_panel(object_panel, "Near", GLUI_SPINNER_FLOAT, &live_nearplane_trans);
	farplane_spin		= glui->add_spinner_to_panel(object_panel, "Far", GLUI_SPINNER_FLOAT, &live_farplane_trans);
	focal_length_spin	= glui->add_spinner_to_panel(object_panel, "Focal", GLUI_SPINNER_FLOAT, &live_focal_length);
	light_intensity_spin = glui->add_spinner_to_panel(object_panel, "Light", GLUI_SPINNER_FLOAT, &live_light_intensity_trans);
	IOD_spin			= glui->add_spinner_to_panel(object_panel, "IOD", GLUI_SPINNER_FLOAT, &live_IOD_trans);
	FOVY_spin			= glui->add_spinner_to_panel(object_panel, "FOVY", GLUI_SPINNER_FLOAT, &live_FOVY_trans);
	
	// rotation and translation controls
	// we need an extra panel to keep things grouped properly
	transform_panel = glui->add_panel_to_panel(scene_rollout, "", GLUI_PANEL_NONE);
	glui->add_column_to_panel(transform_panel, false);
	nearplane_trans		= glui->add_translation_to_panel(transform_panel, "Near Plane", 
		GLUI_TRANSLATION_Y, &live_nearplane_trans);
	nearplane_trans->scale_factor = 0.1;
	farplane_trans		= glui->add_translation_to_panel(transform_panel, "Far Plane", 
		GLUI_TRANSLATION_Y, &live_farplane_trans);
	focal_length_trans	= glui->add_translation_to_panel(transform_panel, "Focal Length", GLUI_TRANSLATION_Y, &live_focal_length);


	glui->add_column_to_panel(transform_panel, false);
	light_intensity_trans = glui->add_translation_to_panel(transform_panel, "Light Intensity", 
		GLUI_TRANSLATION_Y, &live_light_intensity_trans);
	IOD_trans			= glui->add_translation_to_panel(transform_panel, "IOD",
		GLUI_TRANSLATION_Y, &live_IOD_trans);
	FOVY_trans			= glui->add_translation_to_panel(transform_panel, "FOVY", GLUI_TRANSLATION_Y, &live_FOVY_trans);	
	FOVY_trans->scale_factor = 0.01;
	//camera position trackers
	glui->add_column_to_panel(transform_panel, false);
	eye_x_spin			= glui->add_spinner_to_panel(transform_panel, "EyeX", GLUI_SPINNER_FLOAT, &eye[0].x);
	eye_y_spin			= glui->add_spinner_to_panel(transform_panel, "EyeY", GLUI_SPINNER_FLOAT, &eye[0].y);
	eye_z_spin			= glui->add_spinner_to_panel(transform_panel, "EyeZ", GLUI_SPINNER_FLOAT, &eye[0].z);
	lookat_x_spin		= glui->add_spinner_to_panel(transform_panel, "HandX", GLUI_SPINNER_FLOAT, &eye[1].x);
	lookat_y_spin		= glui->add_spinner_to_panel(transform_panel, "HandY", GLUI_SPINNER_FLOAT, &eye[1].y);
	lookat_z_spin		= glui->add_spinner_to_panel(transform_panel, "HandZ", GLUI_SPINNER_FLOAT, &eye[1].z);
	dist1_spin			= glui->add_spinner_to_panel(transform_panel, "Dist1", GLUI_SPINNER_FLOAT, &live_dist[0]);
	dist2_spin			= glui->add_spinner_to_panel(transform_panel, "Dist2", GLUI_SPINNER_FLOAT, &live_dist[1]);
	dist3_spin			= glui->add_spinner_to_panel(transform_panel, "Dist3", GLUI_SPINNER_FLOAT, &live_dist[2]);

	//projection options
	camera_panel = glui->add_panel_to_panel(scene_rollout, "", GLUI_PANEL_NONE);
	projection_radio = glui->add_radiogroup_to_panel(camera_panel, &live_projection_type);
	glui->add_radiobutton_to_group(projection_radio, "glOrtho");
	glui->add_radiobutton_to_group(projection_radio, "gluPerspective");
	glui->add_radiobutton_to_group(projection_radio, "glFrustum");

	pick_radio = glui->add_radiogroup_to_panel(camera_panel, &live_pick_type);
	glui->add_radiobutton_to_group(pick_radio, "GoGo");
	glui->add_radiobutton_to_group(pick_radio, "HeadRay");
	glui->add_radiobutton_to_group(pick_radio, "LookatRay");
	
	// our checkbox options for deciding what to draw
	glui->add_column_to_panel(camera_panel, false);
	draw_floor		= glui->add_checkbox_to_panel(camera_panel, "Draw Floor", &live_draw_floor);
	draw_object		= glui->add_checkbox_to_panel(camera_panel, "Draw Object", &live_draw_object);
	stereo_view		= glui->add_checkbox_to_panel(camera_panel, "Stereo View", &live_stereo_view);
	full_screen		= glui->add_checkbox_to_panel(camera_panel, "Full Screen", 0, CB_FULL_SCREEN, glui_cb);

	wii_rollout			= glui->add_rollout("Wiimote");
	wiimote1_panel			= glui->add_panel_to_panel(wii_rollout, "", GLUI_PANEL_NONE);
	wiimote1_connected	= glui->add_checkbox_to_panel(wiimote1_panel, "Wiimote 1");
	glui->add_column_to_panel(wiimote1_panel, false);
	LED1_1				= glui->add_checkbox_to_panel(wiimote1_panel, "1");
	glui->add_column_to_panel(wiimote1_panel, false);
	LED1_2				= glui->add_checkbox_to_panel(wiimote1_panel, "2");
	glui->add_column_to_panel(wiimote1_panel, false);
	LED1_3				= glui->add_checkbox_to_panel(wiimote1_panel, "3");
	glui->add_column_to_panel(wiimote1_panel, false);
	LED1_4				= glui->add_checkbox_to_panel(wiimote1_panel, "4");
	
	wiimote1_connected->disable();
	LED1_1->disable();
	LED1_2->disable();
	LED1_3->disable();
	LED1_4->disable();

	wiimote2_panel			= glui->add_panel_to_panel(wii_rollout, "", GLUI_PANEL_NONE);
	wiimote2_connected	= glui->add_checkbox_to_panel(wiimote2_panel, "Wiimote 2");
	glui->add_column_to_panel(wiimote2_panel, false);
	LED2_1				= glui->add_checkbox_to_panel(wiimote2_panel, "1");
	glui->add_column_to_panel(wiimote2_panel, false);
	LED2_2				= glui->add_checkbox_to_panel(wiimote2_panel, "2");
	glui->add_column_to_panel(wiimote2_panel, false);
	LED2_3				= glui->add_checkbox_to_panel(wiimote2_panel, "3");
	glui->add_column_to_panel(wiimote2_panel, false);
	LED2_4				= glui->add_checkbox_to_panel(wiimote2_panel, "4");
	wiimote2_connected->disable();
	LED2_1->disable();
	LED2_2->disable();
	LED2_3->disable();
	LED2_4->disable();
	
	// empty space
	wii_tracker_panel = glui->add_panel_to_panel(wii_rollout, "", GLUI_PANEL_NONE);
	LED1_x_spin		= glui->add_spinner_to_panel(wii_tracker_panel, "LED1X", GLUI_SPINNER_INT);
	LED1_y_spin		= glui->add_spinner_to_panel(wii_tracker_panel, "LED1Y", GLUI_SPINNER_INT);
	LED2_x_spin		= glui->add_spinner_to_panel(wii_tracker_panel, "LED2X", GLUI_SPINNER_INT);
	LED2_y_spin		= glui->add_spinner_to_panel(wii_tracker_panel, "LED2Y", GLUI_SPINNER_INT);
	Pitch			= glui->add_spinner_to_panel(wii_tracker_panel, "Pitch", GLUI_SPINNER_FLOAT);
	Roll			= glui->add_spinner_to_panel(wii_tracker_panel, "Roll", GLUI_SPINNER_FLOAT);
	glui->add_column_to_panel(wii_tracker_panel, false);
	Eye_base_x		= glui->add_spinner_to_panel(wii_tracker_panel, "EBaseX", GLUI_SPINNER_FLOAT);
	Eye_base_y		= glui->add_spinner_to_panel(wii_tracker_panel, "EBaseY", GLUI_SPINNER_FLOAT);
	Eye_base_z		= glui->add_spinner_to_panel(wii_tracker_panel, "EBaseZ", GLUI_SPINNER_FLOAT);
	Lookat_base_x	= glui->add_spinner_to_panel(wii_tracker_panel, "LBaseX", GLUI_SPINNER_FLOAT);
	Lookat_base_y	= glui->add_spinner_to_panel(wii_tracker_panel, "LBaseY", GLUI_SPINNER_FLOAT);
	Lookat_base_z	= glui->add_spinner_to_panel(wii_tracker_panel, "LBaseZ", GLUI_SPINNER_FLOAT);

	glui->set_main_gfx_window(main_window);
}

